package com.xiaoyu.lanling.feature.chat.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xiaoyu.base.utils.RxUtils;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.feature.chat.adapter.emoji.ChatEmojiGridAdapter;
import com.xiaoyu.lanling.feature.chat.data.emoji.ChatEmojiData;
import com.xiaoyu.lanling.view.OnShowFragment;

public class ChatEmojiFragment extends OnShowFragment {

    public static ChatEmojiFragment newInstance() {
        Bundle args = new Bundle();
        ChatEmojiFragment fragment = new ChatEmojiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private GridView mGridView;

    private ChatEmojiGridAdapter mChatEmojiGridAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_chat_emoji, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initAdapter();
        initData();
    }

    private void initView(View view) {
        mGridView = view.findViewById(R.id.chat_emoji_grid);
    }

    private void initAdapter() {
        mChatEmojiGridAdapter = new ChatEmojiGridAdapter();
        mGridView.setAdapter(mChatEmojiGridAdapter);
    }

    @SuppressLint("CheckResult")
    private void initData() {
        ChatEmojiData.createChatEmojiList()
                .compose(RxUtils.ioToMainSingleScheduler())
                .subscribe(chatEmojiGridItems -> mChatEmojiGridAdapter.setData(chatEmojiGridItems), Throwable::printStackTrace);
    }

    @Override
    public void onShow() {
    }
}
